import java.awt.image.*;
import java.io.*;

import javax.imageio.ImageIO;

import com.sun.media.jai.codec.*;

public class SeniorProject 
{
	public static void main(String[] args) throws IOException
	{		
		String filename = "comic9/comic9.bmp";
		String output = "comic9/comic9";
		BufferedImage image = ImageIO.read(new File(filename));

		String outputJPG = output + ".jpg";
		String outputPNG = output + ".png";
		
		WritableRaster raster;
		raster = image.getRaster();

		//Encodes image as straight JPEG and PNG
		encodeJPEG(outputJPG, raster, image.getColorModel());
		encodePNG(outputPNG, raster, (RenderedImage) image);

		//This uses the 4-tile technique
		/*NewImage image2 = new NewImage("random1.bmp", 4);
		image2.setOutputFile("random1.txt");
		image2.writeToFileGeneral();
		image2.displaySID();*/
		
		//This uses 8x8 blocks
		NewImage image2 = new NewImage("comic9/comic9.bmp");
		image2.setOutputFile("comic9/comic9.txt");
		image2.writeToFileGeneralBlocks();
		image2.displaySIDBlocks();
	}
	
	//Encodes as JPEG
	public static void encodeJPEG(String outputFile, WritableRaster raster, ColorModel cm) throws IOException
	{
		FileOutputStream out = new FileOutputStream(outputFile);
		JPEGEncodeParam encodeParam = new JPEGEncodeParam();
		ImageEncoder encoder = ImageCodec.createImageEncoder("JPEG", out, encodeParam);
		encoder.encode(raster, cm);
		out.close();
	}
	
	//Encodes as PNG
	public static void encodePNG(String outputFile, WritableRaster raster, RenderedImage image) throws IOException
	{
		FileOutputStream out = new FileOutputStream(outputFile);
		PNGEncodeParam encodeParam = PNGEncodeParam.getDefaultEncodeParam(image);
		ByteArrayOutputStream o = new ByteArrayOutputStream();
		ImageEncoder encoder2 = ImageCodec.createImageEncoder("PNG", o, encodeParam);
		encoder2.encode(raster, image.getColorModel());
		o.writeTo(out);
		out.close();
	}
}
